package com.frogs.ucenter.service.daoimpl;

import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserTaskRecordDao;
import com.frogs.ucenter.entity.UserTaskRecord;
import com.frogs.ucenter.service.UserTaskRecordService;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("userTaskRecordService")
public class UserTaskRecordServiceImpl extends BaseServiceImpl<UserTaskRecord> implements UserTaskRecordService {
    @Autowired
    private UserTaskRecordDao userTaskRecordDao;

    public UserTaskRecordServiceImpl() {
        super();
    }

    public UserTaskRecordDao getUserTaskRecordDao() {
        return userTaskRecordDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<UserTaskRecord> getBaseDao() {
        return userTaskRecordDao;
    }

    public void setUserTaskRecordDao(UserTaskRecordDao userTaskRecordDao) {
        this.userTaskRecordDao = userTaskRecordDao;
    }

	@Override
	public void resendMail(Integer id) throws Exception {
		String sendUrl="http://api.uc.frogshealth.com/v1/user/baseinfo/check.jsonp";
		String[] keyvals = {
				"uid",id.toString(),
				"uuid","mall",
				"token","mall_2014"
			};
		try {
			//userTaskRecordDao.deleteById(id);
			String result = Jsoup.connect(sendUrl).method(Method.POST).data(keyvals).ignoreContentType(true).timeout(5000).execute().body();
			JSONObject object = JSONObject.parseObject(result);
			if (ApiResultEnum.SUCCESS.getId() != object.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName())) {
				throw new ServiceOperationException(object.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			}
		} catch (Exception e) {
			throw new ServiceOperationException(e.getMessage());
		}
	}
}